package com.copyctrl.admin.controller;

import cn.hutool.json.JSONUtil;
import com.copyctrl.admin.service.AdminService;
import com.copyctrl.campus.common.model.ApiResponse;
import com.copyctrl.model.CampAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author:JH
 * @version:1.0
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService service;

    @GetMapping("/{admin}")
    public ApiResponse login(@PathVariable String admin){
        CampAdmin campAdmin = JSONUtil.toBean(admin, CampAdmin.class);
        CampAdmin login = service.login(campAdmin);
        return login==null?ApiResponse.ofFailure("登录错误，请输入正确的账号密码或者联系管理员开通权限"):ApiResponse.ofSuccess(login);
    }

    @PostMapping("/{admin}")
    public ApiResponse register(@PathVariable String admin){
        CampAdmin campAdmin = JSONUtil.toBean(admin, CampAdmin.class);
        int register = service.register(campAdmin);
        return register==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("注册出现问题，请联系管理员");
    }

    @PutMapping("/{admin}")
    public ApiResponse update(@PathVariable String admin){
        CampAdmin campAdmin = JSONUtil.toBean(admin, CampAdmin.class);
        int register = service.updateAdmin(campAdmin);
        return register==1?ApiResponse.ofSuccess("ok"):ApiResponse.ofFailure("修改出现问题，请联系管理员");    }
}
